package logic

import (
	"context"

	"go-zero-mall/app/admin/cmd/rpc/internal/svc"
	"go-zero-mall/app/admin/cmd/rpc/pd"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type ResourceListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewResourceListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResourceListLogic {
	return &ResourceListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 资源-分页
func (l *ResourceListLogic) ResourceList(in *pd.UmsResourceListReq) (*pd.UmsResourceListResp, error) {
	countBuilder := l.svcCtx.UmsResourceModel.CountBuilder("*")
	rowBuilder := l.svcCtx.UmsResourceModel.RowBuilder()
	d, err := l.svcCtx.UmsResourceModel.FindAll(l.ctx, rowBuilder)
	if err != nil {
		return nil, err
	}
	var resp []*pd.UmsResource
	copier.Copy(&resp, d)
	for i := 0; i < len(resp); i++ {
		resp[i].CreateTime = d[i].CreateTime.Time.Unix()
	}
	total, err := l.svcCtx.UmsResourceModel.FindCount(l.ctx, countBuilder)
	if err != nil {
		return nil, err
	}
	return &pd.UmsResourceListResp{Total: total, List: resp}, nil
}
